package com.example.administrator.yuejian35.Recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian35.Club.activity.ClubInfoActivity;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.Recommend.bean.Cancleguanzhu;
import com.example.administrator.yuejian35.Recommend.bean.Guanzhu;
import com.example.administrator.yuejian35.Recommend.fragment.viewholder.GuanzhuItemViewHolder;
import com.example.administrator.yuejian35.Recommend.model.RecommendModel;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.administrator.yuejian35.Config.CourseImages_URL;
import static com.example.administrator.yuejian35.Config.Img_URL;

/**
 * Created by HP on 2017/9/26.
 */

public class GuanzhuListAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Guanzhu> clubList=new ArrayList<>();

    public GuanzhuListAdapter(Context c, List<Guanzhu> clubList){
        this.context = c;
        this.clubList.addAll(clubList);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //1)  将item.xml 生成 itemView  -  View
        //2)  生成MyViewHolderd对象
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_guanzhu,parent,false);
        GuanzhuItemViewHolder holder = new GuanzhuItemViewHolder(itemView);
        return holder;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        //用于绑定数据
        //转换类型
        GuanzhuItemViewHolder clubItemViewHolder= (GuanzhuItemViewHolder) holder;

        clubItemViewHolder.club_name.setText(clubList.get(position).getClubname());
        clubItemViewHolder.cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RecommendModel recommendmodel = new RecommendModel();
                Call call = recommendmodel.cancleguanzhu(clubList.get(position).getCmid());
                call.enqueue(new Callback<Cancleguanzhu>() {
                    @Override
                    public void onResponse(Call<Cancleguanzhu> call, Response<Cancleguanzhu> response) {
                        Toast.makeText(context,"取消成功！", Toast.LENGTH_LONG).show();
                        clubList.remove(position);
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<Cancleguanzhu> call, Throwable t) {

                    }
                });
            }
        });
        clubItemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,ClubInfoActivity.class);
                intent.putExtra("clubid",clubList.get(position).getClubid());
                context.startActivity(intent);
            }
        });
        Picasso.with(context)
                .load(CourseImages_URL+clubList.get(position).getClubpic())
                .into(clubItemViewHolder.club_pic);

    }

    @Override
    public int getItemCount() {
        return clubList.size();
    }
}

